package com.bigdata.demo.service.impl;

import com.bigdata.demo.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Created by root on 3/1/18.
 */
@Service("redisService")
public class RedisServiceImpl implements RedisService {

    private static Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * ½â¾öSpring Boot Ê¹ÓÃRedisTemplate ´æ´¢¼üÖµ³öÏÖÂÒÂë \xac\xed\x00\x05t\x00
     * @param redisTemplate
     */
	@Autowired(required = false)
	public void setRedisTemplate(RedisTemplate redisTemplate) {
		RedisSerializer stringSerializer = new StringRedisSerializer();
		redisTemplate.setKeySerializer(stringSerializer);
		redisTemplate.setValueSerializer(stringSerializer);
		redisTemplate.setHashKeySerializer(stringSerializer);
		redisTemplate.setHashValueSerializer(stringSerializer);
		this.redisTemplate = redisTemplate;
	}

    private static final String PRE_HM_KEY = "AI_KEY@HM#";

    private static final String PRE_KV_KEY = "AI_KEY@KV#";


    @Override
    public boolean putValue(String key, Object value, long expireTime) {
        boolean res = false;
        if (StringUtils.isBlank(key) || null == value) {
            return false;
        }
        try {
            String tempKey = key;
            ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();
            valueOperations.set(tempKey, value);
            redisTemplate.expire(tempKey, expireTime, TimeUnit.SECONDS);
            res = true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("redis»º´æÊý¾Ý¡¾key£º" + key + "¡¿Òì³£", e);
            res = false;
        }
        return res;
    }

    /**
     * »ù±¾Êý¾ÝÀàÐÍµÄÈ¡Öµ¡£
     *
     * @param key
     * @return
     */
    @Override
    public Object getValue(String key) {
        try {
            String tempKey = key;
            Object value =  redisTemplate.opsForValue().get(tempKey);
            return value;
        } catch (Exception e) {
            log.error("redis get value by" + key + " error", e);
            return null;
        }
    }
    @Override
    public Object getList(String key){
        try {
            String tempKey = key;
            Object value =  redisTemplate.opsForList().range(tempKey,0,-1);
            return value;
        } catch (Exception e) {
            log.error("redis get value by" + key + "error", e);
            return null;
        }
    }

    @Override
    public Object getHash(String key){
        try {
            Object value =  redisTemplate.opsForHash().keys(key);
            return value;
        } catch (Exception e) {
            log.error("redis get value by" + key + "error", e);
            return null;
        }
    }

    @Override
    public Object getSet(String key) {
        try {
            String tempKey = key;
            Object value = redisTemplate.opsForSet().members(tempKey);

            return value;
        } catch (Exception e) {
            log.error("redis get value by" + key + "error", e);
            return null;
        }
    }

    @Override
    public Object getZset(String key) {
        try {
            String tempKey = key;
            Object value =  redisTemplate.opsForZSet().range(tempKey,0,-1);
            return value;
        } catch (Exception e) {
            log.error("redis get value by" + key + "error", e);
            return null;
        }
    }

    /**
     * É¾³ý»ù±¾Êý¾ÝÀàÐÍÖÐµÄÖ¸¶¨key¶ÔÓ¦µÄÖµ
     *
     * @param key
     * @return
     */
    @Override
    public boolean removeValue(String key) {
        try {
            String tempKey =  key;
            redisTemplate.delete(tempKey);
            return true;
        } catch (Exception e) {
            log.error("redis»ñÈ¡Êý¾Ý¡¾key£º" + key + "¡¿Òì³£", e);
            return false;
        }
    }

}
